<template>
  <div class="login-container">
    <div class="login_form">
      <el-form class="card-box login-form" autoComplete="off" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
        <h3 class="title">中浩德数据中心</h3>
        <el-form-item prop="username">
          <span class="svg-container svg-container_login">
            <icon-svg icon-class="yonghuming" />
          </span>
          <el-input style="display:none;" name="hidden1" type="text" autoComplete="off" placeholder="请输入用户名" />
          <el-input name="username" type="text" v-model="loginForm.username" autoComplete="off" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <icon-svg icon-class="mima" />
          </span>
          <el-input style="display:none;" name="hidden2" :type="pwdType" autoComplete="off" placeholder="请输入密码" />
          <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="off"
            placeholder="请输入密码" />
          <span class='show-pwd' @click='showPwd'><icon-svg icon-class="yanjing" /></span>
        </el-form-item>

        <el-button type="primary" style="width:100%;margin-bottom:30px;" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
      </el-form>
    </div>
  </div>
</template>
<script>
import { setToken, setTokenName } from "@/utils/auth";
import { loginUrl } from "@/api/keyJob/index";
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        username: "",
        password: ""
      },
      loginRules: {
        //      username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        //      password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      pwdType: "password",
      loading: false,
      showDialog: false
    };
  },
  methods: {
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    async login() {
      try {
        var d = {
          LoginName: this.loginForm.username,
          Pwd: this.loginForm.password
        };
        var res = await loginUrl(d);
        if (res.data.code === "1") {
          var userData = res.data.result;
          this.$localStorage.set("userRouter", JSON.stringify(userData));
          setToken(userData.token);
          this.$store
            .dispatch("GenerateRoutes", {
              userData
            })
            .then(() => {
              this.$router.addRoutes(this.$store.getters.addRouters);
            });
          // var data = this.$utils.getUserInfo(d.LoginName);
          // var roles = [];
          // roles.push(data.role);
          // this.$store.dispatch("GenerateRoutes", { roles }).then(() => {
          //   // 生成可访问的路由表
          //   this.$router.addRoutes(this.$store.getters.addRouters); // 动态添加可访问路由表
          //   // hack方法 确保addRoutes已完成
          // });
          // setToken(data.token);
          // setTokenName(data.name);
          // this.$store.commit("SET_ROLES", data.role);
          // this.$store.commit("SET_NAME", data.name);
          // this.$store.commit("SET_UERINFO", this.loginForm);
          // this.$store.commit("SET_TOKEN", data.token);
          // this.$store.commit("SET_AVATAR", data.avatar);
          // this.$store.commit("SET_INTRODUCTION", data.introduction);
          this.loading = false;
          this.$message({
            message: "登陆成功",
            type: "success",
            duration: 1000
          });
          this.$router.push({ path: "/" });
        } else {
          this.loading = false;
          this.$message({
            message: res.data.msg,
            type: "error",
            duration: 1000
          });
        }
      } catch (error) {
        console.log(error);
        this.loading = false;
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.login();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    afterQRScan() {}
  },
  created() {},
  destroyed() {}
};
</script>

<style scoped>
.login_form .login-form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 10;
}
</style>


<style rel="stylesheet/scss" lang="scss">
@import "src/styles/mixin.scss";
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

/* 修改 样式 start */
body {
  overflow: hidden;
}
.login-container {
  transform: scale(1.2);
}

.login-container .svg-container {
  padding: 0 !important;
  text-align: center !important;
}

.el-form-item__content {
  display: flex;
  justify-content: space-around;
  align-items: center;
}

/* 修改 样式 end */

.login-container {
  @include relative;
  input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px #061f2e inset !important;
    -webkit-text-fill-color: #fff !important;
  }
  input {
    background: transparent;
    border: 0px;
    -webkit-appearance: none;
    border-radius: 0px;
    padding: 12px 5px 12px 15px;
    color: $light_gray;
    height: 47px;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
}
</style>
